﻿using System.ComponentModel.Design;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mogre;

namespace Game
{
    public partial class Form1 : Form
    {
        private bool StopRendering(FrameEvent evt)
        {
            return false;
        }
        public Form1()
        {
            InitializeComponent();

            this.PluginsFileName = @"Content\Plugins.cfg";
            this.ConfigurationFileName = @"Content\MoeWare.cfg";
            this.LogFileName = @"Content\MoeWare.log";
        }

        private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_CheckStateChanged(object sender, EventArgs e)
        {
            
        }

        [DefaultValue(@"Content\Plugins.cfg")]
        public string PluginsFileName { get; set; }
        [DefaultValue(@"Content\MoeWare.cfg")]
        public string ConfigurationFileName { get; set; }
        [DefaultValue(@"Content\MoeWare.log")]
        public string LogFileName { get; set; }
        private void Form1_Load(object sender, EventArgs e)
        {
            //this.propertyGrid1.SelectedObject = this;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            this.GameWorker.ReportProgress(0, GameStatus.PowerOn);
                Root root = new Root(this.PluginsFileName, this.ConfigurationFileName, this.LogFileName);
                Root.Singleton.ShowConfigDialog();
                Root.Singleton.Initialise(true, this.Text);
                //this.propertyGrid1.SelectedObject = Root.Singleton.AutoCreatedWindow;
                Root.Singleton.AutoCreatedWindow.SetVisible(true);
                this.GameWorker.ReportProgress(0, GameStatus.FirstRun);
            Root.Singleton.StartRendering();
            this.GameWorker.ReportProgress(0, GameStatus.LastRun);
            //Root.Singleton.RenderSystem.Shutdown();
                //Root.Singleton.Shutdown();
                root.Dispose();
                this.GameWorker.ReportProgress(0, GameStatus.PowerOff);
            
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //this.PlayGame.Enabled = true;
            //this.StopGame.Enabled = false;
        }


        private void PlayGame_Click(object sender, EventArgs e)
        {
            this.GameWorker.RunWorkerAsync();

            this.PlayGame.Enabled = false;
            //this.StopGame.Enabled = true;
        }

        private void StopGame_Click(object sender, EventArgs e)
        {
            Root.Singleton.FrameEnded+=new FrameListener.FrameEndedHandler(this.StopRendering);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState is GameStatus)
            {
                switch ((GameStatus)e.UserState)
                {
                    case GameStatus.PowerOn:
                        this.PlayGame.Enabled = false;
                        break;
                    case GameStatus.FirstRun:
                        this.StopGame.Enabled = true;
                        //this.PlayGame.Enabled = false;
                        //this.propertyGrid2.SelectedObject = Root.Singleton;
                        this.propertyGrid1.SelectedObject = ResourceGroupManager.Singleton;

                        break;
                    case GameStatus.LastRun:
                        break;
                    case GameStatus.PowerOff:
                        this.PlayGame.Enabled = true;
                        this.StopGame.Enabled = false;
                        break;
                }
            }
        }

        private void LoadScene()
        {
            SceneManager sceneManager = Root.Singleton.CreateSceneManager("MainScene");
            Helper.DotSceneLoader dsl = new Helper.DotSceneLoader();
            SceneNode roomNode = sceneManager.RootSceneNode.CreateChildSceneNode(new Vector3(0, 0, 0));
            dsl.ParseDotScene("room1.scene", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, sceneManager, roomNode);
        }
        private enum GameStatus
        {
            PowerOn,
            PowerOff,
            FirstRun,
            LastRun
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

        }

        private void propertyGrid2_Paint(object sender, PaintEventArgs e)
        {
            
            //this.propertyGrid2.Refresh();
        }

        private void propertyGrid2_Enter(object sender, EventArgs e)
        {
            if (Root.Singleton == null)
            {
                this.propertyGrid2.SelectedObject = null;
            }
            else
            {
                this.propertyGrid2.SelectedObject = Root.Singleton;
            }
        }

        private void propertyGrid2_Click(object sender, EventArgs e)
        {

        }

        private void propertyGrid2_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
        {

        }

        private void propertyGrid2_SelectedObjectsChanged(object sender, EventArgs e)
        {

        }

        private void propertyGrid2_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {

        }
    }
}
